using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Dzzzr;

namespace MonitoringService
{
	/// <summary>
	/// Main WebService class
	/// </summary>
	[WebService(Namespace = "http://dozor.obuhov.ru")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ToolboxItem(false)]
  [ScriptService]
  public class OziService : System.Web.Services.WebService
	{
//    public SecurityCredentials ServicePassword;

//    [WebMethod]
//    [SoapHeader("ServicePassword")]
//    public void Delete(string name, string password)
//    {
//      if(IsValidData())
//        DeleteCar(name, password);
//    }


//    [WebMethod]
//    [SoapHeader("ServicePassword")]
//    public Car[] Ping(string name, string password, double? longitude, double? latitude, double? speed)
//    {
//      if(!IsValidData())
//        return new Car[]{/*new Car("1","2",0,0,0)*/};
//      Car[] lastData = GetLastData();
//      bool wrongPassword = false;
//      foreach (Car car in lastData)
//      {
//        if (car.Name == name && car.Password != password)
//        {
//          wrongPassword = true;
//          break;
//        }
//      } 
//      if(!wrongPassword)
//        SaveData(name, password, longitude, latitude, speed);
//      lastData = GetLastData();
//      return lastData;
//    }

//    /// <summary>
//    /// Get last cars' positions 
//    /// </summary>
//    /// <returns></returns>
//    private Car[] GetLastData()
//    {
//      string commandText =
//        @"SELECT TD.Name, TD.Password, TD.Longitude, TD.Latitude, TD.Time, TD.Speed
//					FROM dbo.TrackData TD
//					JOIN
//						(SELECT [Name],Password,Max(ID) ID
//						FROM TrackData
//						WHERE Time > DateAdd(mi,-@Delay,GetDate())
//						GROUP BY [Name],Password)TDM
//					ON TDM.ID = TD.ID and TD.Deleted = 0";

//      string connString = ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString;
//      List <Car> cars = new List<Car>();
//      using (SqlConnection cnn = new SqlConnection(connString))
//      {
//        cnn.Open();
//        SqlCommand cmd = new SqlCommand(commandText, cnn);
//        cmd.Parameters.Add("@Delay", SqlDbType.Int).Value = ConfigurationManager.AppSettings["Delay"];
//        SqlDataReader rd = cmd.ExecuteReader();
//        while(rd.Read())
//        {
//          double? speed = null;
//          if(rd["Speed"] != DBNull.Value)
//            speed = Convert.ToDouble(rd["Speed"]);

//          cars.Add(new Car(rd["Name"].ToString(),
//                           rd["Password"].ToString(),
//                           Convert.ToDouble(rd["Longitude"]),
//                           Convert.ToDouble(rd["Latitude"]),
//                           speed,
//                           Convert.ToDateTime(rd["Time"])));
//        }
//      }
//      return cars.ToArray();
//    }

//    /// <summary>
//    /// Save car position
//    /// </summary>
//    /// <param name="name"></param>
//    /// <param name="password"></param>
//    /// <param name="longitude"></param>
//    /// <param name="latitude"></param>
//    /// <param name="speed"></param>
//    private void SaveData(string name, string password, double? longitude, double? latitude, double? speed)
//    {
//      if ((longitude == null) || (latitude == null))
//        return;
//      string commandText =
//        @"INSERT INTO [TrackData]
//       ([Name]
//       ,[Password]
//       ,[Longitude]
//       ,[Latitude]
//			 ,[Speed]
//       ,[Time])
//				VALUES
//        (@Name,
//				@Password,
//				@Longitude,
//				@Latitude,
//				@Speed,
//				GetDate())";
//      string connString = ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString;
//      using(SqlConnection cnn = new SqlConnection(connString))
//      {
//        cnn.Open();
//        SqlCommand cmd = new SqlCommand(commandText, cnn);
//        cmd.Parameters.AddWithValue("@Name", name);
//        cmd.Parameters.AddWithValue("@Password", password);
//        cmd.Parameters.AddWithValue("@Longitude", longitude);
//        cmd.Parameters.AddWithValue("@Latitude", latitude);
//        cmd.Parameters.AddWithValue("@Speed", speed == null ? (object)DBNull.Value : (object)speed);
//        cmd.ExecuteNonQuery();
//      }
//    }

//    /// <summary>
//    /// Validate user rights
//    /// </summary>
//    /// <returns></returns>
//    private bool IsValidData()
//    {
//      return true;
//      //return ServicePassword != null && ServicePassword.Password == ConfigurationManager.AppSettings["ServicePassword"];
//    }

//    /// <summary>
//    /// Delete car by name and password
//    /// </summary>
//    /// <param name="name"></param>
//    /// <param name="password"></param>
//    private void DeleteCar(string name, string password)
//    {
//      string commandText =
//        @"UPDATE [TrackData] SET Deleted = 1
//					WHERE [Name] = @Name and Password = @Password";

//      string connString = ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString;
//      using (SqlConnection cnn = new SqlConnection(connString))
//      {
//        cnn.Open();
//        SqlCommand cmd = new SqlCommand(commandText, cnn);
//        cmd.Parameters.AddWithValue("@Name", name);
//        cmd.Parameters.AddWithValue("@Password", password);
//        cmd.ExecuteNonQuery();
//      }

//    }

	}

	public class SecurityCredentials : SoapHeader
	{
		public string Password;
	}
}
